#include<iostream>
#include<cstdlib>

using namespace std;

//head files of Linux
#include<netinet/in.h>
#include<unistd.h>   //for fork and read
#include<sys/types.h>   //for socket
#include<sys/socket.h>  //for socket
#include<string.h> // for bzero
#include<arpa/inet.h>

struct send_info
{
    char length; // 8 char
    char command;
    float data1;
    float data2;
    float data3;
    float data4;

    float data5;
    float data6;
    float data7;
    float data8;

    char data9;
    float data10;

    char data11;
    char data12;
//    char data13;
}; // float 4 char 9个
   // long int 8 char 5个
   // int 4 char 
   // 72

char* float2char(float data)
{
    char c[4];
    char *pc = (char*)&data;

    for(int i=0; i<4; i++)
         c[i] = *(pc+i); 

    return c;   
}

void server()
{
    const unsigned short SERVERPORT = 5000;
    const char* SERVER_IP = "192.168.1.30";
    int sock, client_fd;
    sockaddr_in myAddr;
    sockaddr_in remoteAddr;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    //create socket
    if( sock == -1)
    {
        cerr<<"socket create fail!"<<endl;
        exit(1);
    }

    cout<<"sock :"<<sock<<endl;

    //bind
    myAddr.sin_family = AF_INET;
    myAddr.sin_port = htons(5000);
    myAddr.sin_addr.s_addr = inet_addr("192.168.1.100");

    connect(sock, (sockaddr*)&myAddr,sizeof(sockaddr));
    
    char snd_buf[1024];
    while(true)
    {
        struct send_info info1; //定义结构体变量
        info1.length = 12;
        info1.command = 13;
        info1.data1 = 21.34;
        info1.data2 = 3.5;
        info1.data3 = 4.6;
        info1.data4 = 5.7;
        info1.data5 = 6.8;
        info1.data6 = 7.9;
        info1.data7 = 8.1;
        info1.data8 = 9.2;
        info1.data9 = 10.3;
        info1.data10 = 11.4;
        info1.data11 = 12;
        info1.data12 = 13;

        memset(snd_buf,0,1024);

        memcpy(snd_buf,&info1,sizeof(info1)-2); //结构体转换成字符串
       
        snd_buf[2] = float2char(info1.data1)[0];
        snd_buf[3] = float2char(info1.data1)[1];
        snd_buf[4] = float2char(info1.data1)[2];
        snd_buf[5] = float2char(info1.data1)[3];

        snd_buf[6] = float2char(info1.data2)[0];
        snd_buf[7] = float2char(info1.data2)[1];
        snd_buf[8] = float2char(info1.data2)[2];
        snd_buf[9] = float2char(info1.data2)[3];

        snd_buf[10] = float2char(info1.data3)[0];
        snd_buf[11] = float2char(info1.data3)[1];
        snd_buf[12] = float2char(info1.data3)[2];
        snd_buf[13] = float2char(info1.data3)[3];

        snd_buf[14] = float2char(info1.data4)[0];
        snd_buf[15] = float2char(info1.data4)[1];
        snd_buf[16] = float2char(info1.data4)[2];
        snd_buf[17] = float2char(info1.data4)[3];
        

        snd_buf[18] = float2char(info1.data5)[0];
        snd_buf[19] = float2char(info1.data5)[1];
        snd_buf[20] = float2char(info1.data5)[2];
        snd_buf[21] = float2char(info1.data5)[3];
        

        snd_buf[22] = float2char(info1.data6)[0];
        snd_buf[23] = float2char(info1.data6)[1];
        snd_buf[24] = float2char(info1.data6)[2];
        snd_buf[25] = float2char(info1.data6)[3];

        snd_buf[26] = float2char(info1.data7)[0];
        snd_buf[27] = float2char(info1.data7)[1];
        snd_buf[28] = float2char(info1.data7)[2];
        snd_buf[29] = float2char(info1.data7)[3];
        

        snd_buf[30] = float2char(info1.data8)[0];
        snd_buf[31] = float2char(info1.data8)[1];
        snd_buf[32] = float2char(info1.data8)[2];
        snd_buf[33] = float2char(info1.data8)[3]; // 9

        snd_buf[34] = (char)info1.data9; // 9

        snd_buf[35] = float2char(info1.data10)[0];
        snd_buf[36] = float2char(info1.data10)[1];
        snd_buf[37] = float2char(info1.data10)[2];
        snd_buf[38] = float2char(info1.data10)[3];

        snd_buf[39] = (char)info1.data11; // 9
        snd_buf[40] = (char)info1.data12; // 9


        send(sock,snd_buf,41,0);//发送信息

        cout << sizeof(info1)-2<< endl;

        usleep(500000);
    }

    close(sock);
}       


int main()
{
    // send message 
    server();
}



